package com.csun.dsms.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 20:19
 * @description: TODO
 */
public class DateUtil {

    /** 年月日时分秒(无下划线) yyyyMMddHHmmss */
    public static final String dtLong = "yyyyMMddHHmmssSSS";

    public final static DateFormat YYYY_MM_DD_MM_HH_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public final static DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");

    public final static DateFormat YYYY_MM = new SimpleDateFormat("yyyy-MM");

    public final static DateFormat YYYY = new SimpleDateFormat("yyyy");

    public static String getOrderNum() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat(dtLong);
        return df.format(date);
    }

    public static String getNowTime() {

        return YYYY_MM_DD_MM_HH_SS.format(new Date());
    }

    public static String getTodayDate() {

        return YYYY_MM_DD.format(new Date());
    }

    public static String getNowMonth() {

        return YYYY_MM.format(new Date());
    }

    public static int getDaysOfMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static Date strToDate(String dateString){
        Date date = null;
        try {
            date = YYYY_MM_DD_MM_HH_SS.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getNowYear() {

        return YYYY.format(new Date());
    }

    public static String dateToStr(Date date){
        return YYYY_MM_DD_MM_HH_SS.format(date);
    }

    public static String dateToStr1(Date date){
        return YYYY_MM_DD.format(date);
    }


    public static Date str2Date(String dateString){
        Date date = new Date();
        try {
            if (dateString != null && !dateString.equals("")) {
                dateString = dateString.replace("年","-");
                dateString = dateString.replace("月","-");
                dateString = dateString.replace("日","");
                dateString = dateString.replace("/","-");
                dateString = dateString.replace(".","-");
                if (dateString.length() <=12) {
                    date = YYYY_MM_DD.parse(dateString);
                }else {
                    date = YYYY_MM_DD_MM_HH_SS.parse(dateString);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
